<?php

/**
 * 检查备注和旗标
 *
 * @author hzjsq@msn.com
 * @version 0.1b
 */
class omeauto_auto_plugin_store extends omeauto_auto_plugin_abstract implements omeauto_auto_plugin_interface {

    /**
     * 是否支持批量审单
     */
    protected $__SUP_REP_ROLE = true;

    /**
     * 状态码
     */
    protected $__STATE_CODE = omeauto_auto_const::__STORE_CODE;

    /**
     * 开始处理
     *
     * @param omeauto_auto_group_item $group 要处理的订单组
     * @return Array
     */
    public function process(& $group, &$confirmRoles) {
        $allow = true;
        $groupStore = $this->getGroupStore($group);
        $bids = $group->getBranchId();
        $isStoreBranch = $group->isStoreBranch();

        //门店库存逻辑处理
        if (app::get('o2o')->is_installed() && $bids && $isStoreBranch) {
            //检查门店仓库存情况
            $allow = $this->checkStoreBranchStore($bids, $group, $groupStore);
        }elseif($bids){
            //检查电商仓库存情况
            $allow = $this->checkElectricBranchStore($bids, $group, $groupStore);
        }else{
            $allow = false;
        }
        
        if (!$allow) {
            foreach($group->getOrders() as $order) {
                $group->setOrderStatus($order['order_id'], $this->getMsgFlag());
            }
            $group->setStatus(omeauto_auto_group_item::__OPT_HOLD, $this->_getPlugName());
        }
    }
    
    /**
     * 获取订单组中的所有货品数及货品编号
     * 
     * @param omeauto_auto_group_item $group
     * @return void
     */
    private function getGroupStore($group) {
        
        $result = array('store' => array(), 'pids' => array());
        foreach($group->getOrders() as $order) {
            foreach($order['objects'] as $objects){
                foreach ($objects['items'] as $item) {
                    
                    if (in_array($item['product_id'], $result['pids'])) {
                        //已经存在
                        $result['store'][$item['product_id']] += $item['nums'];
                    } else {
                        //没有新的
                        $result['pids'][] = $item['product_id'];
                        $result['store'][$item['product_id']] = $item['nums'];
                    }
                }
            }
        }
        
        return $result;
    }

    //根据门店仓检查库存情况
    private function checkStoreBranchStore($branchIds, $group, $groupStore){
        $storeManageLib    = kernel::single('ome_store_manage');
        $branchProductObj  = app::get('o2o')->model('branch_product');
        
        //根据获取到的仓库来判断库存
        foreach ((array) $branchIds as $bid )
        {
            $storeManageLib->loadBranch(array('branch_id'=>$bid));
            $isCtrlStore    = $storeManageLib->isCtrlBranchStore();
            
            if(!$isCtrlStore){
                $group->setBranchId($bid);
                return true;
            }else{
                //检查基础物料的供货关系是否存在
                $bm_ids    = array();
                $is_flag   = true;
                
                foreach ($groupStore['pids'] as $bm_id)
                {
                    $bmRow    = $branchProductObj->dump(array('bm_id'=>$bm_id, 'branch_id'=>$bid, 'status'=>1), 'bm_id, is_ctrl_store');
                    
                    if(empty($bmRow))
                    {
                        //供货关系不存在,跳出
                        $is_flag    = false;
                        break;
                    }
                    
                    //管控库存的基础物料
                    if($bmRow['is_ctrl_store'] == 1)
                    {
                        $bm_ids[]    = $bmRow['bm_id'];
                    }
                }
                
                //供货关系不存在,判断下一个仓库
                if(!$is_flag)
                {
                    continue;
                }
                
                //找到门店供货的基础物料中需要管控库存的基础物料
                $allow    = true;
                if($bm_ids)
                {
                    //实际库存减掉冻结数再做库存判断
                    $sql      = "SELECT bm_id,sum(IF(store<store_freeze,0,store-store_freeze)) AS store FROM sdb_o2o_product_store WHERE bm_id in (".join(',', $bm_ids).") AND branch_id = ".$bid." group by bm_id";
                    $prows    = $branchProductObj->db->select($sql);
                    
                    //转换数据格式
                    $store = array();
                    foreach ((array) $prows as $row) {
                        $store[$row['bm_id']] = $row;
                    }
                    
                    //检查订单组内的货品数量是否足够
                    foreach ($groupStore['store'] as $pid => $nums) {
                        if(in_array($pid, $bm_ids)){
                            if (($store[$pid]['store'] - $nums) < 0) {
                                $allow = false;
                            }
                        }
                    }
                }
                
                //成功
                if ($allow) {
                    $group->setBranchId($bid);
                    return true;
                }
            }
        }
        
        if (!$allow || ($is_flag === false)) {
            return false;
        }
    }

    //根据电商仓检查库存情况
    private function checkElectricBranchStore($bids, $group, $groupStore){
        //检查每个订单库存是否充足
        $branchObj = app::get('ome')->model("branch");
        //根据获取到的仓库来判断库存
        foreach ((array) $bids as $bid ) {
            $delivBranch = $branchObj->getDelivBranch($bid);
            $branchIds = array();
            $branchIds = $delivBranch[$bid]['bind_conf'];
            $branchIds[] = $bid;
            if ($bid > 0) {
                $sql = "SELECT product_id,sum(IF(store<store_freeze,0,store-store_freeze)) AS store FROM sdb_ome_branch_product WHERE product_id in (".join(',', $groupStore['pids']).") AND branch_id IN (".implode(',', $branchIds).") group by product_id";
                $prows = kernel::database()->select($sql);
                //转换数据格式
                $store = array();
                foreach ((array) $prows as $row) {
                    $store[$row['product_id']] = $row;
                }
                
                //检查订单组内的货品数量是否足够
                $allow = true;
                foreach ($groupStore['store'] as $pid => $nums) {
                    if (($store[$pid]['store'] - $nums) < 0) {
                        $allow = false;
                    } 
                }
                
                if ($allow) {
                    $group->setBranchId($bid);
                    return true;
                }
            }
        }

        if (!$allow) {
            return false;
        }
    }

    /**
     * [此方法没有地方调用]获取订单中所有的产品IDS
     * 
     * @param Array $order
     * @return Array  
     */
    private function getAllProductIds($order) {

        $ids = array();
        
        //物料版需要先读取objects层数据
        foreach($order['objects'] as $objects)
        {
            foreach ($objects['items'] as $item) {
    
                $ids[] = $item['product_id'];
            }
        }

        return $ids;
    }

    /**
     * 获取该插件名称
     *
     * @param Void
     * @return String
     */
    public function getTitle() {

        return '库存不足';
    }

    /**
     * 获取提示信息
     *
     * @param Array $order 订单内容
     * @return Array
     */
    public function getAlertMsg(& $order) {

        return array('color' => '#3E3E3E', 'flag' => '库', 'msg' => '库存不足');
    }

    /**
     * 获取用于快速审核的选项页，输出HTML代码
     * 
     * @param void
     * @return String
     */
    public function getInputUI() {
        
    }

}